<?php

use HjCommon\Utility;

class ApiAction extends Action{

	function test(){
		$hash = $_GET['hash'];
		
		if(empty($hash)){
			$hash = '3db4e600ebdf';
		}
		$ob = M("csl_customer");
		
			$info = $ob->where(array('hash'=>$hash))->find();
			var_dump($info['cid']);
	}

	function getCustomer(){
		
		$hash = $_GET['hash'];
		
		$ob = M("csl_customer");
		
		if(strlen($hash)<5){
			$info = $ob->where(array('sourceid'=>$hash))->find();
		}else{
			$info = $ob->where(array('hash'=>$hash))->find();
		}
		
		echo json_encode($info);
	}

	function getProductType(){
		$result = array();
		$ob = M("csl_dict");
		if($_GET['ctype']=='1'){
			$type_in = "p_in_cus";
			$type_out = "p_out_cus";
			$type_img = "p_zg_img";      //尊贵款轮播图
		}elseif($_GET['ctype']=='2'){
			$type_in = "p_in_default";
			$type_out = "p_out_default";
			$type_img = "p_jd_img";  //经典版轮播图
		}
		$listIn = $ob->where(array('type'=>$type_in))->select();
		$listOut = $ob->where(array('type'=>$type_out))->select();
		$listImg = $ob->where(array('type'=>$type_img))->order('code asc')->select();

		$result['in'] = $listIn;
		$result['out'] = $listOut;
		$result['p_img'] = $listImg;

		echo json_encode($result);
	}

	function saveOrder(){
		
// 		echo json_encode($_POST);
// 		exit();
		if ($_POST){
			$orderNo = '';
			
			$_POST['mobile'] = $_POST['shipping_mobile']; //删除下单账号，使用快递电话当账号
			
			if($_POST['oid'] == ''){  //新增
	            $no = createOrderNo();
	            $randNum = rand(1000,9999);
	            $_POST['order_no'] = $no.$randNum;  //订单号
	            $orderNo = $no.$randNum; 
	        }
	        
	        $hash = $_GET['hash'];
	        session("hash", $hash);
		/*	if(empty($hash)){
				$hash = '3db4e600ebdf';
			}*/
	
			$ob = M("csl_customer");
			$info = $ob->where(array('hash'=>$hash))->find();
			 
			$_POST['uid'] = $info['uid'];//下单人所属门店
			
			$cust = $ob->where(array('mobile'=>trim($_POST['mobile'])))->find();
			if ($cust){  
				//如果存在该账户，则订单计入该账号
				$_POST['cid'] = $cust['cid'];//下单人
				
			}else{
				//不存在就新增
				$data['mobile'] = $_POST['mobile'];
				$data['uid'] =  $info['uid'];
				$data['parentcid'] =  $info['cid'];
				$data['ctime'] = date('Y-m-d H:i:s', time());
				$cid = $ob->add($data);
				$_POST['cid'] = $cid;
				CommonFunc::Log("新增客户[{$cid} / ".$_POST['mobile']."] 客户来源:[{$info['cid']} / {$info['mobile']}] 分配代理商：[{$info['uid']}] 分配消费者上级：[{$info['cid']}]",'customer');
			}
		
	        // $_POST['order_item_price'] = 44;
	        // $_POST['order_price'] = $_POST['number']*$_POST['order_item_price'];			//订单总金额
	        
	        if(empty($_POST['order_status'])){
	            $_POST['order_status'] = '1';//订单状态
	        }
	/*        $upload = $this->uploadCard();
	        if($upload){
	            $_POST['order_img'] = $upload;
	        }*/
	        	
	        $_POST['ctime'] = date('Y-m-d H:i:s',time());		//生成时间
	        $_POST['order_coupon_price'] = 0;
	        $_POST['order_shipping_cost'] = 0;
	        $data = $_POST;
	        
	        //新增加选择款属性
	        $data['product_outside_type'] = $_POST['typeOutside'];//新增加选择款属性
	        $data['product_inside_type'] = implode(',', $_POST['typeInside']);//新增加选择款属性
	        
	        $data['product_outside_name'] = $_POST['typeOutsideName'];//新增加选择款属性
	        $data['product_inside_name'] = implode(',', $_POST['typeInsideName']);//新增加选择款属性
	        
	        
// 	        1:名片地址 2:其他地址
	        if($data['shipping_type']=='1'){
// 	        	'shipping_province': '',
// 	        	'shipping_city' : '',
// 	        	'shipping_distinct' : '',
// 	        	'shipping_address' : '',
	        	$data['shipping_province'] = "";
	        	$data['shipping_city'] = "";
	        	$data['shipping_distinct'] = "";
	        	$data['shipping_address'] = "使用名片上的地址";
	        }
	        $ob = M("csl_order");
	        if($_POST['oid'] != ''){
	            $ob->where(array('oid'=>$_POST['oid']))->save($data);
	        }else{
	            $ob->add($data);
	        }
	        
	        // $result = array('ststus'=>'1', 'msg'=>'success');
	        $result = array();
	        $result['status'] = '1';
	        $result['msg'] = 'success';
	        $result['hash'] = $hash;
	        $result['order_no'] = $orderNo;
	        
	        echo json_encode($result);

		}
	}
	
	function updateOrder(){
		$ob = M("csl_order");
		
		if($_POST['order_no'] != ''){
			$ob->where(array('order_no'=>$_POST['order_no']))->save($_POST);
			
			$result = array();
			$result['status'] = '1';
			$result['msg'] = 'success';
				
			echo json_encode($result);
		}else{
			$result = array();
			$result['status'] = '-1';
			$result['msg'] = '订单号为空';
			
			echo json_encode($result);
		}
		 
		
	}
	
	/**
	 * 更新提现银行卡信息
	 */
	function updateUserInfo(){
		$ob = M("csl_customer");
		
		if($_POST['cid'] != ''){
			$ob->where(array('cid'=>$_POST['cid']))->save($_POST);
				
			$result = array();
			$result['status'] = '1';
			$result['msg'] = 'success';
		
			echo json_encode($result);
		}else{
			$result = array();
			$result['status'] = '-1';
			$result['msg'] = 'cid为空';
				
			echo json_encode($result);
		}
	}
	
	/**
	 * 前端提现
	 */
	function cashdetail(){
		$ob = M("csl_cash");
		
		$db = M("csl_customer");
		
		if($_GET['yzm'] != ''){
			if ($_GET["yzm"] == session("msgCode")){
				if ($_POST["money"] - $_POST["tixianmoney"] >= 0){//判断账号金额是否足够
					$oldmoney = $_POST["money"];  //原本金额
					$_POST["uid"] = 0;
					$_POST["money"] = $_POST["tixianmoney"];
					$_POST["ctime"] = date("Y-m-d H:i:s", time());
					$_POST["status"] = "0";
					$ob->add($_POST);
					
					//提现之后扣除金额
					$data["money"] = $oldmoney - $_POST["tixianmoney"];
					$db->where(array("cid"=>$_POST["cid"]))->save($data);
					
					//保存提现日志
					LogTransactionModel::ModifyMoney("", $_POST["cid"], "提现", $oldmoney, '-'.$_POST["tixianmoney"], $data["money"], "提现操作成功");
					
					$result = array("status"=>"1","msg"=>"提现成功");
					echo json_encode($result);
					return;
				}else{
					$result = array("status"=>"-1","msg"=>"金额不足");
					echo json_encode($result);
					return;
				}
			}else{
				$result = array("status"=>"-1","msg"=>"验证码错误");
				echo json_encode($result);
				return;
			}
		}else{
			$result = array("status"=>"-1","msg"=>"验证码为空");
			echo json_encode($result);
			return;
		}
	}

}


?>